{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "19dd560d",
   "metadata": {},
   "source": [
    "# Python使用API模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6e6a3a50",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "3bc1e5fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'ac0YXaZmbh0HVE-zbgiFXJi9oxoNQoPL'\n",
    "API_sercret = 'Q3BtiKl8a8x5No1IgQCtmW-NYTXjEFPz'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://img0.baidu.com/it/u=462212056,2661937251&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=375'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "91979051",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "d57b6d76",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1648455951,25adbc4f-a1c4-4505-a3a9-9e2a7f501a84\",\"time_used\":278,\"faces\":[{\"face_token\":\"f5fb60bb7ba63164525176af1c84c863\",\"face_rectangle\":{\"top\":253,\"left\":130,\"width\":259,\"height\":259}}],\"image_id\":\"iVNu5MuXUL1+S+JIpEA7VA==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "df17c1f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'f5fb60bb7ba63164525176af1c84c863'"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "25a6e45c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'f5fb60bb7ba63164525176af1c84c863'"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token = r.json()['faces'][0]['face_token']\n",
    "face_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e31f76a0",
   "metadata": {},
   "source": [
    "# 人脸分析API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6ec18fdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "analyze_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'ac0YXaZmbh0HVE-zbgiFXJi9oxoNQoPL'\n",
    "API_sercret = 'Q3BtiKl8a8x5No1IgQCtmW-NYTXjEFPz'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':face_token,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = analyze_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "6bb8715c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "2a836da9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648455951,25adbc4f-a1c4-4505-a3a9-9e2a7f501a84',\n",
       " 'time_used': 278,\n",
       " 'faces': [{'face_token': 'f5fb60bb7ba63164525176af1c84c863',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "c39d5af9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "466222e1",
   "metadata": {},
   "source": [
    "## 数据处理小魔法 pandas.json_normalize() 实现数据表格化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "9862beaa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>f5fb60bb7ba63164525176af1c84c863</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>259</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>259</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                      0\n",
       "face_token             f5fb60bb7ba63164525176af1c84c863\n",
       "face_rectangle.top                                  253\n",
       "face_rectangle.left                                 130\n",
       "face_rectangle.width                                259\n",
       "face_rectangle.height                               259"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r.json()['faces']).T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "837faa64",
   "metadata": {},
   "source": [
    "# 人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57fa3d85",
   "metadata": {},
   "source": [
    "##  人脸检测获取toekn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "1c475fbe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'ac0YXaZmbh0HVE-zbgiFXJi9oxoNQoPL'\n",
    "API_sercret = 'Q3BtiKl8a8x5No1IgQCtmW-NYTXjEFPz'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://img0.baidu.com/it/u=462212056,2661937251&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=375'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "d2298246",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'f5fb60bb7ba63164525176af1c84c863'"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token01 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "75c2ee5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'ac0YXaZmbh0HVE-zbgiFXJi9oxoNQoPL'\n",
    "API_sercret = 'Q3BtiKl8a8x5No1IgQCtmW-NYTXjEFPz'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=1'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "f5a835f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'f5fb60bb7ba63164525176af1c84c863'"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5db65955",
   "metadata": {},
   "source": [
    "## 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61341b73",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'ac0YXaZmbh0HVE-zbgiFXJi9oxoNQoP'\n",
    "API_sercret = 'Q3BtiKl8a8x5No1IgQCtmW-NYTXjEFPz'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_token1':face_token01,\n",
    "    'face_token2':face_token02\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = compare_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "8028366d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "8203f8f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648455951,25adbc4f-a1c4-4505-a3a9-9e2a7f501a84',\n",
       " 'time_used': 278,\n",
       " 'faces': [{'face_token': 'f5fb60bb7ba63164525176af1c84c863',\n",
       "   'face_rectangle': {'top': 253, 'left': 130, 'width': 259, 'height': 259}}],\n",
       " 'image_id': 'iVNu5MuXUL1+S+JIpEA7VA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "020345cc",
   "metadata": {},
   "source": [
    "# 封装人脸检测函数，实现快速调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "7b82d6ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'ac0YXaZmbh0HVE-zbgiFXJi9oxoNQoP'\n",
    "API_sercret = 'Q3BtiKl8a8x5No1IgQCtmW-NYTXjEFPz'\n",
    "img_url_01 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=1'\n",
    "img_url_02 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=ImgDet&rs=2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "78f61de2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_sercret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "bdc65773",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_message': 'AUTHENTICATION_ERROR'}"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "56884c4a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_message': 'AUTHENTICATION_ERROR'}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_02)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
